Completed
Push — master ( bc1ad9...b87221 )
by Askupa
01:45
created

Amarkal.settings.reset   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * Asynchronously save all settings in the current settings page to the database.
3
 * Shows an error notification if errors occur. Shows a success notification
4
 * otherwise.
5
 * 
6
 * @param {Function} done
7
 */
8
Amarkal.settings.save = function( done ) 
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
{
10
    Amarkal.settings._postData('save',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
        
12
        Amarkal.settings._clearErrors();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
        
14
        if(!$.isEmptyObject(res.errors)) {
15
            for(var name in res.errors) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
16
                var $comp = $('[amarkal-component-name="'+name+'"]');
17
                
18
                $comp.amarkalUIComponent('makeInvalid');
19
                $comp.parent()
20
                     .children('.amarkal-settings-error')
21
                     .addClass('amarkal-visible')
22
                     .html(res.errors[name]);
23
            }
24
            Amarkal.settings.notifier.error('Some errors have occured, see below for more information.');
25
        }
26
        else {
27
            Amarkal.settings.notifier.success('Settings saved', 3000);
28
        }
29
30
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
31
        
32
        done();
33
    });
34
};
35
36
/**
37
 * Asynchronously reset all settings in the current settings page to their 
38
 * default values and erase all data from the database. Shows a success 
39
 * notification upon completion.
40
 * 
41
 * @param {Function} done
42
 */
43
Amarkal.settings.resetAll = function( done ) 
44
{
45
    Amarkal.settings._postData('reset_all',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
        
47
        Amarkal.settings._clearErrors();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
48
        Amarkal.settings.notifier.success('Default settings applied', 3000);
49
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
50
        
51
        done();
52
    });
53
};
54
55
/**
56
 * Asynchronously reset all settings in the current settings section to their 
57
 * default values and erase all section data from the database. Shows a success 
58
 * notification upon completion.
59
 * 
60
 * @param {Function} done
61
 */
62
Amarkal.settings.resetSection = function( done ) 
63
{
64
    Amarkal.settings._postData('reset_section',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
65
        
66
        Amarkal.settings._clearErrors();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
67
        Amarkal.settings.notifier.success('Default settings applied for the section <strong>'+Amarkal.settings.sections.getTitle(Amarkal.settings.sections.activeSection)+'</strong>', 3000);
68
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
69
        
70
        done();
71
    });
72
};
73
74
/**
75
 * Reset all components and clear errors
76
 */
77
Amarkal.settings._clearErrors = function()
78
{
79
    // Reset all components
80
    $('.amarkal-ui-component').amarkalUIComponent('reset');
81
    $('.amarkal-settings-error').removeClass('amarkal-visible').html('');
82
};
83
84
/**
85
 * Send serialized form data to be processed in the backend by the function given
86
 * in the 'action' variable.
87
 * 
88
 * @param {string} action
89
 * @param {Function} done
90
 */
91
Amarkal.settings._postData = function( action, done )
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
92
{
93
    var data = $('#amarkal-settings-form').amarkalUIForm('getData');
94
    $('#amarkal-settings-form').find('input[name^="_amarkal"]').each(function(){
95
        data[$(this).attr('name')] = $(this).val();
96
    });
97
98
    // Set the active section (if applicable)
99
    data['_amarkal_settings_section'] = Amarkal.settings.sections.activeSection;
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
100
101
    $.post(ajaxurl, {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
102
        action: 'amarkal_settings_'+action,
103
        data: data
104
    }, done);
105
};